const JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;
const keys = require("../config/key")
var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrkey;

const mongoose = require("mongoose");
// 链接users 实例
const User = mongoose.model("users");
module.exports = passport => {
        passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
            User.findById(jwt_payload.id)
                .then(user => {
                    if (user) {
                        return done(null, user);

                    } else {
                        return done(null, false)
                    }
                })
                .catch(err => console.log(err))

        }))
    }
    // ppeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVlNjlmZjJhYTllZjgyNGI5NDkzNzMwYiIsIm5hbWUiOiJodW8iLCJpYXQiOjE1ODQwMDQ5NDMsImV4cCI6MTU4NDAwODU0M30.dWMKTV5ZhL8hy1CHj2qCUFnROBL_vE9MFjIU2qWLCS0